<?php

namespace app\adm\controller;

class Deal extends Base
{
	public function are_int () 
	{
	    $args = func_get_args ();
	    foreach ( $args as $arg )
	        if ( (! is_int ( $arg )) && ($arg<1))
	            return false;
	    return true;
	}
	// 矿机类型
	public function cmType()
	{
		if (is_post()) {
			$post = input('post.');
			if (input('get.acc') == 'edit') {
				if ($this->are_int($post['lifetime'],$post['phased'],$post['candy_num'],$post['price'])) {
					if (!($post['lifetime'] % $post['phased'])) {
						$post['num'] = $post['lifetime'] / $post['phased'];
						$residue = 100 % $post['num'];
						$int =floor(100 / $post['num']);
						$diff_arry = array();
						array_push($diff_arry,$int+$residue);
						for ($i = 1; $i<=$post['num']-1; $i++) {
							array_push($diff_arry,$int);
						}
						$post['different'] = implode(',', $diff_arry);
						if ($post['type_id']) {
							$rst = db('candym_type')->where('type_id', $post['type_id'])->update($post);
							if ($rst) {
								$this->success('操作完成');
							}
							$this->error('刷新重试');
						} else {
							$rst = db('candym_type') -> insert($post);
							if ($rst) 
								return $this->success('添加成功');
							return $this->error('请稍后再试');
						}
					}
					$this -> error('总周期比起为展期的整数倍');
				}
				$this -> error('请填入有效值');
			}
			if (input('get.acc') == 'diff') {
				$id = $post['type_id'];
				unset($post['type_id']);
				$diff =array();
				$last = 100;
				foreach ($post as $v) {
					$last = $last - $v;
					if ($v) array_push($diff,$v);
				}
				array_push($diff, $last);
				$str = implode(',',$diff);
				$rst = db('candym_type')->where('type_id',$id)->update(['different'=>$str]);
				if ($rst) $this -> success('修改成功'); $this -> error('稍后重试');
			}
			if (input('get.acc') == 'status') {
				$status = db('candym_type')->where('type_id',input('post.id'))->value('status');
				$status = $status ? 0 : 1;
				$rst = db('candym_type')->where('type_id',input('post.id'))->setField('status',$status);
				if($rst) $this->success('修改成功');$this->error('修改失败');
			}
		}

		if (input('get.type_id')) {
			$info = db('candym_type')->where('type_id', input('get.type_id'))->find();
			$info['diff'] = explode(',', $info['different']);
			return $info;
		}
		$arr = db('item_cate')->select();
		$items = [];
		foreach ($arr as $k =>$v) {
			$items[$v['id']] = $v['cate_name'];
		}
		// halt($items);
		$this->assign('items',$items);
		$list = db('candym_type')
			-> paginate()
			-> each(function ($item, $key) {
				$item['different'] = explode(',', $item['different']);
				return $item;
		});
		$this->assign('list', $list);
		return $this->fetch();
	}



	// 糖果机列表 candy machine
	public function cmlist()
	{
		$map = [];
		if (is_get()) {
			if (input('get.keywords')) {
				$map['name|tel'] = ['like', '%'.input('get.keywords').'%'];
			}
			if (is_numeric(input('get.status'))) {
				$map['candy_machine.status'] = input('get.status');
			}
			if (input('get.uid')) {
				$map['candy_machine.uid'] = input('get.uid');
			}
			if (input('get.type')) {
				$map['candy_machine.type'] = input('get.type');
			}
		}

		$list  = model('CandyMachine')->getList($map);
		// halt($list);
		$this->assign('cmType', db('candym_type')->field('type_id,type_name')->select());
		$this->assign('list', $list);

		return $this->fetch();
	}

	// 代理充值
	public function dlChong()
	{	
		$map = [];
		$map['type'] = 2;
		if (is_get()) {
			if (input('get.uid')) {
				$map['uid'] = input('get.uid');
			}
			if (input('get.status')) {
				$map['status'] = input('get.status');
			}
			if (input('get.start') && input('get.end')) {
				$map['create_time'] = ['between',[input('get.start'),input('get.end')]];
 			}
		}

		$list  = model('UserAgent')->getList($map);
		$this->assign('list', $list);
		return $this->fetch();
	}

	// 玩家转账
	public function zhuan()
	{
		if (is_post()) {
			$id = input('post.id');
			// 代替玩家收款
			if (input('get.act') == 'confirm') {
				return model('UserAgent')->confirm($id, session('ad_name'));
			}
			if (input('get.act') == 'cancle') {
				return model('UserAgent')->reject($id, session('ad_name'));
			}
		}

		$map = [];
		$map['type'] = 1;
		if (is_get()) {
			if (input('get.uid')) {
				$map['uid'] = input('get.uid');
			}
			if (input('get.status')) {
				$map['status'] = input('get.status');
			}
			if (input('get.start') && input('get.end')) {
				$map['create_time'] = ['between',[input('get.start'),input('get.end')]];
 			}
		}

		$list  = model('UserAgent')->getList($map);

		$this->assign('list', $list);

		return $this->fetch();
	}

	// 卖出记录
	public function sellList()
	{
		if (is_post()) {
			$id = input('post.id');
			// 代替玩家收款
			if (input('get.act') == 'confirm') {
				return model('UserTrade')->sysConfirm($id, session('ad_name'));
			}
			if (input('get.act') == 'cancle') {
				return model('UserTrade')->reject($id, session('ad_name'));
			}
		}

		$map = [];
		if (is_get()) {
			if (input('get.uid')) {
				$map['uid'] = input('get.uid');
			}
			if (input('get.status')) {
				$map['status'] = input('get.status');
			}
		}

		$list = model('UserTrade')->getList($map);
		$this->assign('list', $list);

		return $this->fetch();
	}

	// 买入记录
	public function buyList()
	{
		$map = [];
		if (is_get()) {
			if (input('get.uid')) {
				$map['uid'] = input('get.uid');
			}
			if (input('get.status')) {
				$map['status'] = input('get.status');
			}
		}

		$list = model()->getList($map);
		$this->assign('list', $list);

		return $this->fetch();
	}
	
}
